<?php

require_once dirname(__FILE__).'/../lib/sfGuardUserGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/sfGuardUserGeneratorHelper.class.php';

/**
 * sfGuardUser actions.
 *
 * @package    sfGuardPlugin
 * @subpackage sfGuardUser
 * @author     Fabien Potencier
 * @version    SVN: $Id: actions.class.php 23319 2009-10-25 12:22:23Z Kris.Wallsmith $
 */
class sfGuardUserActions extends autoSfGuardUserActions
{
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

    $user = $this->getRoute()->getObject();

    $this->auditDelete($this->getRoute()->getObject());

    if ($this->getRoute()->getObject()->delete())
    {
      $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
    }

    $this->redirect('@sf_guard_user');
  }

  protected function executeBatchDelete(sfWebRequest $request)
  {
    $ids = $request->getParameter('ids');

    $records = Doctrine_Query::create()
      ->from('sfGuardUser')
      ->whereIn('id', $ids)
      ->execute();

    foreach ($records as $record)
    {
      $this->auditDelete($record);
      $record->delete();
    }

    $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
    $this->redirect('@sf_guard_user');
  }

  protected function auditDelete($record)
  {
    $aud = new auditoria();
    $aud->setTabla('sf_guard_user');
    $aud->setIds($record->getId());
    $aud->setAnterior($record->getFirstName().'; '.$record->getLastName().'; '.$record->getUsername().'; '.$record->getEmailAddress());
    $aud->setUser($this->getUser()->getGuardUser());
    $aud->setAccion('BAJA');
    $aud->setFechaYHora(date('Y-m-d H:i:s'));
    $aud->save();
  }
}
